package models

import (
	"context"
	"go-chain-api/global"
	"go.mongodb.org/mongo-driver/bson"
)

type WithdrawVerify struct {
	Id     string `bson:"_id,omitempty" json:"id"`
	UserId string `bson:"userId" json:"userId"`
	Status int    `bson:"status" json:"status"` // 0 不需要审核 1.需要审核
}

func SetStatus(userId string, status int) error {
	client := global.DBEngine.Database(global.DbConfig.DbName).Collection("withdraw_verify")
	filter := bson.M{"userId": userId}
	update := bson.M{
		"$set": bson.M{
			"status": status,
		},
	}
	var err error

	count, _ := client.CountDocuments(context.TODO(), filter)
	if count == 0 {
		var data WithdrawVerify
		data.Status = status
		data.UserId = userId
		_, err = client.InsertOne(context.TODO(), data)

	} else {
		_, err = client.UpdateOne(context.Background(), filter, update)
	}

	return err
}

func GetStatus(userId string) int {
	client := global.DBEngine.Database(global.DbConfig.DbName).Collection("withdraw_verify")
	filter := bson.M{"userId": userId}

	var data WithdrawVerify
	err := client.FindOne(context.TODO(), filter).Decode(&data)
	if err != nil {
		return 1
	} else {
		return data.Status
	}

}

func GetInfoByUserId(userId string) (WithdrawVerify, error) {
	client := global.DBEngine.Database(global.DbConfig.DbName).Collection("withdraw_verify")
	filter := bson.M{"userId": userId}

	var data WithdrawVerify
	err := client.FindOne(context.TODO(), filter).Decode(&data)
	return data, err
}
